package likou;

import java.util.HashMap;

public class countGood {
    public long countGood(int[] nums, int k) {
        int count=0;
        long ans=0;
        HashMap<Integer,Integer> hash=new HashMap<>();
        for(int left=0,right=0;right<nums.length;right++){
            hash.put(nums[right],hash.getOrDefault(nums[right],0)+1);
            int n=hash.get(nums[right]);
            count=count+n-1;
            while(count>=k){
                ans+=nums.length-right;
                hash.put(nums[left],hash.getOrDefault(nums[left],0)-1);
                int m=hash.get(nums[left]);
                count-=m;
                left++;
            }
        }
        return ans;
    }
}
